<script setup lang="ts">
import {
  UserControllerService,
  UserLoginRequest,
  UserRegisterRequest,
} from "../../../generated";
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import message from "@arco-design/web-vue/es/message";

const isRegister = ref(false);

const loginData = reactive({
  userAccount: "",
  userPassword: "",
} as UserLoginRequest);

const registerData = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
} as UserRegisterRequest);

const router = useRouter();
const store = useStore();

const login = async () => {
  const res = await UserControllerService.userLoginUsingPost(loginData);
  // 登录成功，跳转到主页
  if (res.code === 0) {
    await store.dispatch("user/getLoginUser");
    router.push({
      path: "/questions",
      replace: true,
    });
  } else {
    message.error("登陆失败，" + res.message);
  }
};

const register = async () => {
  const res = await UserControllerService.userRegisterUsingPost(registerData);
  if (res.code === 0) {
    isRegister.value = false;
  } else {
    message.error("注册失败，" + res.message);
  }
};

const togglePanel = () => {
  isRegister.value = !isRegister.value;
};
</script>

<template>
  <div class="login-page">
    <div class="mask"></div>
    <div class="container" :class="{ 'panel-active': isRegister }">
      <!-- 注册 -->
      <div class="container-form container-signup">
        <form class="form" ref="registerFormRef">
          <h2 class="form-title">注册账号</h2>
          <input
            type="text"
            v-model="registerData.userAccount"
            placeholder="账号"
            class="input"
          />
          <input
            type="password"
            v-model="registerData.userPassword"
            placeholder="密码"
            class="input"
          />
          <input
            type="password"
            v-model="registerData.checkPassword"
            placeholder="确认密码"
            class="input"
          />
          <button type="button" class="btn" @click="register">点击注册</button>
        </form>
      </div>

      <!-- 登录 -->
      <div class="container-form container-signin">
        <form class="form" ref="loginFormRef">
          <h2 class="form-title">欢迎登录</h2>
          <input
            type="text"
            v-model="loginData.userAccount"
            placeholder="账号"
            class="input"
          />
          <input
            type="password"
            v-model="loginData.userPassword"
            placeholder="密码"
            class="input"
          />
          <a href="#" class="link">忘记密码</a>
          <button type="button" class="btn" @click="login">登录</button>
        </form>
      </div>

      <!-- 叠加层部分 -->
      <div class="container-overlay">
        <div class="overlay">
          <div class="overlay-panel overlay-left">
            <button class="btn" @click="togglePanel">已有账号？去登录</button>
          </div>
          <div class="overlay-panel overlay-right">
            <button class="btn" @click="togglePanel">没有账号？去注册</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 登录页面背景 */
.login-page {
  position: relative; /* 让子元素可以基于它定位 */
  height: 100vh;
  background: url("@/assets/1.jpg") center no-repeat fixed;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 蒙版层（略小于整个背景） */
.mask {
  position: absolute;
  width: 90vw; /* 调整大小，使其略小于背景 */
  height: 90vh;
  background: rgba(231, 231, 231, 0.2); /* 半透明 */
  backdrop-filter: blur(5px); /* 使其模糊 */
  border-radius: 10px; /* 添加圆角 */
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.3); /* 让模糊层更有层次感 */
}

/* 让 container 在蒙版上层 */
.container {
  position: relative;
  z-index: 2;
  background-color: rgba(231, 231, 231, 0.8); /* 让容器也有轻微透明度 */
  border-radius: 0.7rem;
  height: 420px;
  max-width: 750px;
  width: 100%;
  padding: 20px;
}
/* 登录、注册框部分 */
.container-form {
  height: 100%;
  position: absolute;
  top: 0;
  transition: all 0.6s ease-in-out;
}
/* 登录框 - 默认层级高 */
.container-signin {
  left: 0;
  width: 50%;
  z-index: 2;
}
/* 注册框 - 默认层级低 - 透明度 0 */
.container-signup {
  left: 0;
  opacity: 0;
  width: 50%;
  z-index: 1;
}
/* 表单样式 */
.form {
  background-color: #e7e7e7;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 3rem;
  height: 100%;
  text-align: center;
}
.form-title {
  font-weight: 300;
  margin: 0;
  margin-bottom: 1.25rem;
}
.link {
  color: #333;
  font-size: 0.9rem;
  margin: 1.5rem 0;
  text-decoration: none;
}
.input {
  width: 100%;
  background-color: #fff;
  padding: 0.9rem 0.9rem;
  margin: 0.5rem 0;
  border: none;
  outline: none;
}
.btn {
  background-color: #f25d8e;
  box-shadow: 0 4px 4px rgba(255, 112, 159, 0.3);
  border-radius: 5px;
  color: #e7e7e7;
  border: none;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: bold;
  letter-spacing: 0.1rem;
  padding: 0.9rem 4rem;
  text-transform: uppercase;
  transition: transform 80ms ease-in;
}
.form > .btn {
  margin-top: 1.5rem;
}
.btn:active {
  transform: scale(0.95);
}
/* ---------- 叠加部分样式 ------------- */
.container-overlay {
  height: 100%;
  left: 50%;
  overflow: hidden;
  position: absolute;
  top: 0;
  transition: transform 0.6s ease-in-out;
  width: 50%;
  z-index: 100;
}
.overlay {
  width: 200%;
  height: 100%;
  position: relative;
  left: -100%;
  background: url("@/assets/1.jpg") no-repeat center fixed;
  background-size: cover;
  transition: transform 0.6s ease-in-out;
  transform: translateX(0);
}
.overlay-panel {
  height: 100%;
  width: 50%;
  position: absolute;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}
.overlay-left {
  transform: translateX(-20%);
}
.overlay-right {
  right: 0;
  transform: translateX(0);
}
/* 设计激活时叠加层的位置 */
.panel-active .overlay-left {
  transform: translateX(0);
}
.panel-active .container-overlay {
  transform: translateX(-100%);
}
.panel-active .overlay {
  transform: translateX(50%);
}
/* 设置激活时，登录注册层的位置和透明度 */
.panel-active .container-signin {
  transform: translateX(100%);
}
.panel-active .container-signup {
  opacity: 1;
  z-index: 5;
  transform: translateX(100%);
}
</style>
